<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Nouns</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="../classics.css" rel="stylesheet" type="text/css">
</head>

<?php
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}

$nouns = array();

function addNoun($sNom, $stem, $type) {
	global $nouns;
	$nouns[$sNom]['stem'] = $stem;
	$nouns[$sNom]['type'] = $type;
}


$endings['default'] = array('Singular' => array('Nom.' => '', 'Gen.' => 'is', 'Dat.' => 'ī', 'Acc.' => 'em', 'Abl.' => 'e'),
	                        'Plural' => array('Nom.' => 'ēs', 'Gen.' => 'um', 'Dat.' => 'ibus', 'Acc.' => 'ēs', 'Abl.' => 'ibus'));
$endings['lr_n']['Singular']['Acc.'] = '';
$endings['lr_n']['Plural']['Nom.'] = 'a';
$endings['lr_n']['Plural']['Acc.'] = 'a';

$endings['n_n']['Singular']['Acc.'] = '';
$endings['n_n']['Plural']['Nom.'] = 'a';
$endings['n_n']['Plural']['Acc.'] = 'a';

$endings['s_n']['Singular']['Acc.'] = '';
$endings['s_n']['Plural']['Nom.'] = 'a';
$endings['s_n']['Plural']['Acc.'] = 'a';

$endings['is']['Singular']['Abl.'] = array('ī', 'e');
$endings['is']['Singular']['Acc.'] = array('im', 'em');
$endings['is']['Plural']['Gen.'] = 'ium';
$endings['is']['Plural']['Acc.'] = array('īs', 'ēs');

$endings['ealar']['Singular']['Acc.'] = '';
$endings['ealar']['Singular']['Abl.'] = 'ī';
$endings['ealar']['Plural']['Nom.'] = 'ia';
$endings['ealar']['Plural']['Gen.'] = 'ium';
$endings['ealar']['Plural']['Acc.'] = 'ia';

$endings['mixed']['Plural']['Gen.'] = 'ium';
$endings['mixed']['Plural']['Acc.'] = array('ēs', 'īs');


addNoun('trabs', 'trab', 'bp');
addNoun('prīnceps', 'prīncip', 'bp');
addNoun('lapis', 'lapid', 'dt');
addNoun('mīles', 'mīlit', 'dt');
addNoun('aetās', 'aetāt', 'dt');
addNoun('rēx', 'rēg', 'gc');
addNoun('rādīx', 'rādīc', 'gc');
addNoun('dux', 'duc', 'gc');
addNoun('cōnsul', 'cōnsul', 'lr');
addNoun('victor', 'victōr', 'lr');
addNoun('aequor', 'aequor', 'lr_n');
addNoun('leō', 'leōn', 'n');
addNoun('virgō', 'virgin', 'n');
addNoun('nōmen', 'nōmin', 'n_n');
addNoun('mōs', 'mōs', 's');
addNoun('honor', 'honōs', 's');
addNoun('corpus', 'corpos', 's_n');

addNoun('turris', 'turr', 'is');
addNoun('īgnis', 'īgn', 'is');
addNoun('hostis', 'host', 'is');

addNoun('cubīle', 'cubīl', 'ealar');
addNoun('animal', 'animāl', 'ealar');
addNoun('calcar', 'calcār', 'ealar');

addNoun('nūbēs', 'nūb', 'mixed');
addNoun('urbs', 'urb', 'mixed');
addNoun('arx', 'arc', 'mixed');
addNoun('imber', 'imbr', 'mixed');

$Nom = array_rand($nouns);
$stem = $nouns[$Nom]['stem'];
$type = $nouns[$Nom]['type'];


$vtab = "<caption>".$Nom."</caption>\n";
$vtab = $vtab . "<tr><td></td><th align=\"center\">Singular</th><th align=\"center\">Plural</th></tr>\n";

foreach(array('Nom.', 'Gen.', 'Dat.', 'Acc.', 'Abl.') as $case) {
	$vtab = $vtab . "<tr><th>$case</th>";
	foreach(array('Singular', 'Plural') as $number) {
		$vtab = $vtab . "<td align=\"center\">";
		
		if (isset($endings[$type][$number][$case])) {
			if (is_array($endings[$type][$number][$case])) {
				foreach ($endings[$type][$number][$case] as $ending) {
					if (mb_strlen($ending) > 0) {
						$txts[] = $stem.$ending;
					} else {
						$txts[] = $Nom;
					}
					$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
				}
			} else {
				$ending = $endings[$type][$number][$case];
				
				if (mb_strlen($ending) > 0) {
					$txts[] = $stem.$ending;
				} else {
					$txts[] = $Nom;
				}
				$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
			}
		} else {
			$ending = $endings['default'][$number][$case];
				
			if (mb_strlen($ending) > 0) {
				$txts[] = $stem.$ending;
			} else {
				$txts[] = $Nom;
			}
			$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
		}
		

		$vtab = $vtab . "</td>";
	}
	$vtab = $vtab . "</tr>\n";
}
?>

<script type="text/javascript">
function check() 
{

<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "if (document.n01.text".$i.".value=='".$txts[$i]."')\n{\n";
		print "\tdocument.n01.text".$i.".disabled=true;\n}\n";
	}
?>

}

function newWord() 
{
<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "document.n01.text".$i.".disabled=false;\n";
 		print "document.n01.text".$i.".value='';\n";
	}
?>
	location.reload();
}
</script>

<body>
<div id="container">
<div id="center">
<form name="n01" action="">
<table align="center">
<?php
	print $vtab;
?>
<tr><td></td>
	<td align="center"><input name="Check" onclick="check()" value="Check" type="button"></td>
	<td align="center"><input name="New Word" onclick="newWord()" value="New Word" type="button"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
